//
//  GNNewfeatureViewController.swift
//  SinaWeChat
//
//  Created by mac on 15/12/9.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit


let SCREENW = UIScreen.mainScreen().bounds.width
let SCREENH = UIScreen.mainScreen().bounds.height

class GNNewfeatureViewController: UIViewController ,UIScrollViewDelegate{
    
    override func viewDidLoad() {
        
        
        setupUI()
        
    }
    
    
    //视图初始化
    
    private func setupUI(){
        
        //添加scrollView
        scrollView.frame = view.bounds
        
        view.addSubview(scrollView)
        pageControl.frame = CGRect(x: 0.5 * SCREENW, y: SCREENH - 100, width: 0, height: 0)
        view.addSubview(pageControl)
        //往其中加入图片
        
        let count = 4
        
        for i in 0..<count {
            
            //初始化imageview
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            //设置大小
            imageView.frame = CGRect(x: CGFloat(i) * SCREENW , y: 0, width: SCREENW, height: SCREENH)
            
            scrollView.addSubview(imageView)
            
            //判断如果是最后一页,设置最后一页的内容
            if i == count - 1 {
                
                setupLastPage(imageView)
            }
            
        }
        
        //设置内容大小
        scrollView.contentSize = CGSize(width: CGFloat(count) * SCREENW, height: 0)
        
        //页数
        
        pageControl.numberOfPages = count
    
    }
    
    
    //初始化设置最后一页的内容
    
    private func setupLastPage(lastpage:UIView){
        
        //最后一页开启用户主界面
        lastpage.userInteractionEnabled = true
        
        enterButton.center = CGPoint(x: lastpage.frame.width * 0.5, y: lastpage.frame.height - 150)
        
        shareButton.center = CGPoint(x: enterButton.center.x, y: CGRectGetMinY(enterButton.frame) - 30)
        
        //添加子控件
        lastpage.addSubview(enterButton)
        lastpage.addSubview(shareButton)
        
    }
    
    //MARK:-按钮的点击事件
    
    @objc private func shareButtonclick(button:UIButton){
        
        
        button.selected = !button.selected
        
    }
    
    @objc private func enterButtonclick(){
        
        
        //执行进入主页的逻辑
        
    }
    
    //MARK:- scrollView delegate
    
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        //计算当前滑到第几页
        let page = scrollView.contentOffset.x / SCREENW + 0.5
        
        pageControl.currentPage = Int(page)
    }
    
    //MARK :- 懒加载控件
    private lazy var scrollView : UIScrollView = {
        
        let scrollView = UIScrollView()
        
        //设置代理
        scrollView.delegate = self
        //开启分页
        scrollView.pagingEnabled = true
        
        //关闭边缘的弹簧效果
        scrollView.bounces = false
        
        //隐藏滚动条
        
        scrollView.showsHorizontalScrollIndicator = false
        
        return scrollView
        
        }()
    
    //分页控件
    private lazy var pageControl : UIPageControl = {
        
       let pageControl = UIPageControl()
        
        //若不指定clip to bounds 则无需关心跨高
        
        //指定选中颜色
        pageControl.currentPageIndicatorTintColor = UIColor.redColor()
        
        //指定默认颜色
        pageControl.pageIndicatorTintColor = UIColor.lightGrayColor()
        
        return pageControl
    }()
    
    
    //进入按钮
    
    private lazy var enterButton : UIButton = {
       
        let button = UIButton()
        
        button .addTarget(self, action: "enterButtonclick", forControlEvents: .TouchUpInside)
        
        
        //设置不同状态的图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        
        button.setTitle("进入微博", forState: .Normal)
        
        //自适应
        button.sizeToFit()
        return button
    }()
    
    private lazy var shareButton : UIButton = {
        
        let button = UIButton()
        
        button.addTarget(self, action: "shareButtonclick:", forControlEvents: .TouchUpInside)
        
        //设置不同状态的图片
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        
        button.setTitle("分享到微博", forState:.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        
        button.sizeToFit()
        return button
    }()
}
